package com.itheima.sc.impl.pms;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.sc.api.pms.ProductAttributeApi;
import com.itheima.sc.mapper.pms.ProductAttributeMapper;
import com.itheima.sc.pojo.pms.Brand;
import com.itheima.sc.pojo.pms.ProductAttribute;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class ProductAttributeImpl extends ServiceImpl<ProductAttributeMapper, ProductAttribute> implements ProductAttributeApi {

    @Override  //attributeCatory中的删除,相对应的attribute里根据catoryId删除
    public void deleteByCategoryId(Integer cid) {
        LambdaQueryWrapper<ProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductAttribute::getProductAttributeCategoryId, Convert.toLong(cid));
        //删除cid对应的attribute
        remove(wrapper);
    }

    @Override //获取cid对应的attr集合, 获取所有category时设置其附加属性
    public List<ProductAttribute> listByCategoryId(Long id) {
        LambdaQueryWrapper<ProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductAttribute::getProductAttributeCategoryId,id);

        List<ProductAttribute> attributeList = list(wrapper);
        return attributeList;
    }

    @Override   //分页根据分类查询属性列表或参数列表
    public List<ProductAttribute> listByType(Integer cid, Integer type, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductAttribute::getType,type);
        wrapper.eq(ProductAttribute::getProductAttributeCategoryId,cid);

        Page<ProductAttribute> pageInfo = new Page<>(pageNum, pageSize);
        pageInfo = page(pageInfo, wrapper);
        List<ProductAttribute> attributeList = pageInfo.getRecords();

        return attributeList;
    }

    @Override
    public Integer getCount(Integer cid, Integer type, Integer pageNum, Integer pageSize) {
        LambdaQueryWrapper<ProductAttribute> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductAttribute::getType,type);
        wrapper.eq(ProductAttribute::getProductAttributeCategoryId,cid);

        int count = count(wrapper);
        return count;
    }
}
